<?php
namespace Home\Controller;
use Home\Controller\CommonController;
class IndexController extends CommonController { //index是模块名,默认加载view层的index模块下的模板

    //我的项目
    public function index(){
        $Form   =  M('project');
        $Stage   =  M('stage');
        $data =  $Form->join('fms_stage ON fms_project.sid = fms_stage.sid')->order('pid desc')->select(); // 读取数据
        $data2 = $Stage->getField('sname',true);
//        echo "<pre>";print_r($data);exit;
        if($data && $data2) {
            $data3['amount'] = 0;
            $data3['estimate'] = 0;
            foreach($data as $key => $item){   //计算停滞天数
                $time1 = strtotime($item['updtime']);
                $time2 = strtotime(date("Y-m-d"));
                $data[$key]['days'] = round(abs($time1 - $time2)/86400);
                $data3['amount'] += $item['amount'];
                $data3['estimate'] += $item['amount']*$item['rate']*0.01;
            }
            $data3['count'] = count($data);
            // $this->assign($data); // 更直接地给模板变量赋值
            $this->data = $data;// 模板变量赋值
            $this->data2 = $data2;
            $this->data3 = $data3;
            $this->title = 'FMS - 我的项目';
        }
        $this->display();   //默认加载fms模块下的myProject模板
    }

    //新增商机
    public function addProject(){
        $Stage = M('stage');
        $data = $Stage->select();
        if($data){
            $this->data = $data;
            $this->title = 'FMS - 添加商机';
            $this->display();
        }
        else{
            $this->error('网络异常！');
        }
    }

    public function stageConf(){
        $this->title = 'FMS - 设置阶段';
        $this->display('header');
        $this->display();
        $this->display('footer');
    }

    public function myReport(){
        $this->title = 'FMS - 我的报表';
        $this->display('header');
        $this->display();
        $this->display('footer');
    }

    public function history(){
        $this->title = 'FMS - 历史记录';
        $this->display('header');
        $this->display();
        $this->display('footer');
    }

    public function team(){
        $this->title = 'FMS - 团队';
        $this->display('header');
        $this->display();
        $this->display('footer');
    }

    public function teamReport(){
        $this->title = 'FMS - 团队报表';
        $this->display('header');
        $this->display();
        $this->display('footer');
    }

    public function estimate(){
        $this->title = 'FMS - 预计';
        $this->display('header');
        $this->display();
        $this->display('footer');
    }

    public function busi(){
        $this->title = 'FMS - 商机';
        $this->display('header');
        $this->display();
        $this->display('footer');
    }

    public function aboutUs(){
        $this->title = 'FMS - 关于我们';
        $this->display('header');
        $this->display();
        $this->display('footer');
    }

    public function delete($id=''){
        $Form = M('Project');
        $result = $Form->delete($id);
        if($result){
            $this->success('操作成功!');
        }
        else{
            $this->error('失败!');
        }
    }

}